package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CamsEnrollInfo;
import com.ruoyi.system.domain.CamsRecord;
import com.ruoyi.system.domain.vo.ActivityVo;
import com.ruoyi.system.service.ICamsEnrollInfoService;
import com.ruoyi.system.service.ICamsRecordService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.mapper.CamsActivityMapper;
import com.ruoyi.system.domain.CamsActivity;
import com.ruoyi.system.service.ICamsActivityService;

/**
 * 校园活动Service业务层处理
 *
 * @author anskory
 * @date 2024-04-07
 */
@Service
public class CamsActivityServiceImpl extends ServiceImpl<CamsActivityMapper, CamsActivity> implements ICamsActivityService {
    @Autowired
    private CamsActivityMapper camsActivityMapper;

    @Autowired
    private ICamsEnrollInfoService camsEnrollInfoService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ICamsRecordService camsRecordService;

    /**
     * 查询校园活动列表
     *
     * @param camsActivity 校园活动
     * @return 校园活动
     */
    @Override
    public List<CamsActivity> selectCamsActivityList(CamsActivity camsActivity) {
        LambdaQueryWrapper<CamsActivity> lambdaQueryWrapper = new LambdaQueryWrapper<CamsActivity>();
        if (!SecurityUtils.isAdmin()) lambdaQueryWrapper.eq(CamsActivity::getCreateBy, SecurityUtils.getUsername());
        if (camsActivity.getTitle() != null && camsActivity.getTitle() != "")
            lambdaQueryWrapper.like(CamsActivity::getTitle, camsActivity.getTitle());
        if (camsActivity.getOutline() != null && camsActivity.getOutline() != "")
            lambdaQueryWrapper.like(CamsActivity::getOutline, camsActivity.getOutline());
        if (camsActivity.getResponsiblePerson() != null && camsActivity.getResponsiblePerson() != "")
            lambdaQueryWrapper.like(CamsActivity::getResponsiblePerson, camsActivity.getResponsiblePerson());
        lambdaQueryWrapper.orderByDesc(CamsActivity::getCreateTime);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public List<ActivityVo> signUpList(String keyword) {
        LambdaQueryWrapper<CamsEnrollInfo> ceiLQW = new LambdaQueryWrapper<CamsEnrollInfo>();
        ceiLQW.eq(CamsEnrollInfo::getUsername, SecurityUtils.getUsername());
        List<CamsEnrollInfo> camsEnrollInfoList = camsEnrollInfoService.list(ceiLQW);
        List<String> activityIdList = camsEnrollInfoList.stream().map(CamsEnrollInfo::getActivityId).toList();
        LambdaQueryWrapper<CamsActivity> lambdaQueryWrapper = new LambdaQueryWrapper<CamsActivity>();
        if (StringUtils.isNotBlank(keyword)) {
            lambdaQueryWrapper.like(CamsActivity::getTitle, keyword);
        }
        lambdaQueryWrapper.in(CamsActivity::getId, activityIdList);
        List<CamsActivity> camsActivities = this.list(lambdaQueryWrapper);
        List<ActivityVo> activityVoList = new ArrayList<>();
        BeanUtils.copyProperties(activityVoList, camsActivities);
        //判断camsEnrollInfoList的activityId是否与activityVoList的id相同,如果相同，则将CamsEnrollInfo塞入activityVoList的enrollInfo中，不能嵌套for循环
        for (CamsEnrollInfo camsEnrollInfo : camsEnrollInfoList) {
            for (ActivityVo activityVo : activityVoList) {
                if (camsEnrollInfo.getActivityId().equals(activityVo.getId())) {
                    activityVo.setEnrollInfo(camsEnrollInfo);
                }
            }
        }
        return activityVoList;

//            return camsActivityMapper.signUpList(keyword);
//        return null;
    }

    @Override
    public void testTask() {
        System.out.println("测试定时任务");
    }

    @Override
    public AjaxResult signUp(String id) {
        CamsActivity camsActivity = this.getById(id);
        LambdaQueryWrapper<CamsEnrollInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CamsEnrollInfo::getActivityId, id);
        long count = camsEnrollInfoService.count(queryWrapper);
        if (camsActivity.getStartTime().after(DateUtils.getNowDate())) {
            return AjaxResult.error("报名未开始！");
        }
        if (camsActivity.getEndTime().before(DateUtils.getNowDate())) {
            return AjaxResult.error("报名已结束！");
        }
        if (count > camsActivity.getUpperLimit() && camsActivity.getUpperLimit() != 0) {
            return AjaxResult.error("人数已满！");
        }
        if (count > 0) {
            return AjaxResult.error("请勿重复报名！");
        }
        CamsEnrollInfo camsEnrollInfo = new CamsEnrollInfo();
        camsEnrollInfo.setActivityId(id);
        camsEnrollInfo.setUsername(SecurityUtils.getUsername());
        camsEnrollInfo.setEnrollTime(DateUtils.getNowDate());
        boolean save = camsEnrollInfoService.save(camsEnrollInfo);
        if (!save) {
            return AjaxResult.error("报名失败,请联系管理员");
        } else {
//            SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
//            System.out.println(sysUser.getPhonenumber());
//            System.out.println(camsActivity.getTitle());
//            System.out.println(camsActivity.getStartTime().toString());
//            boolean b = new AliyunSmsUtil().noticeSms(sysUser.getPhonenumber(), camsActivity.getTitle(), DateUtils.dateTime(camsActivity.getStartTime()));
            return AjaxResult.success("报名成功");
        }
    }

    /**
     * 获取已发布活动详情
     *
     */
    @Override
    public CamsActivity releaseDetail(CamsActivity camsActivity) {
        LambdaQueryWrapper<CamsRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CamsRecord::getActivityId, camsActivity.getId());
        List<CamsRecord> list1 = camsRecordService.list(wrapper);
        List<CamsRecord> handUps = camsRecordService.filterRecordsByType(list1, 1);
        List<CamsRecord> collects = camsRecordService.filterRecordsByType(list1, 2);
        camsActivity.setViewCount(camsRecordService.filterRecordsByType(list1, 0).size());
        camsActivity.setHandUpList(handUps);
        camsActivity.setCollectList(collects);
        camsActivity.setIsHandUp(handUps.stream().anyMatch(x1 -> Objects.equals(x1.getUsername(), SecurityUtils.getUsername())));
        camsActivity.setIsCollect(collects.stream().anyMatch(x1 -> Objects.equals(x1.getUsername(), SecurityUtils.getUsername())));
        camsActivity.setHandUpCount(handUps.size());
        camsActivity.setCollectCount(collects.size());
        return camsActivity;
    }

}
